const app = getApp()
const api = app.globalData.api
Page({
    data: {
        item: [],
        from: '',
        warehouseOption: [],
        warehousePopup: false,
        positionOption: [],
        positionPopup: false,
        key: '',
    },
    // 获取仓库
    getWarehouse: async function () {
      let res = await api.curlGet('?cla=inStock&fun=getWarehouse')
      if (res.code === 1) {
          this.setData({
            warehouseOption: res.data
          })
      }
    },
    // 获取库位
    getPosition: async function (warehouseId, positionId) {
      if(warehouseId){
        let that = this
        let res = await api.curlPost('?cla=inStock&fun=getPosition', {warehouseId: warehouseId, positionId, positionId})
        if (res.code === 1) {
          that.setData({
            positionOption: res.data.positionOption,
            ['item.position']: res.data.position,
            positionId: res.data.positionId,
          })
        }
      }
    },
    main: async function () {
      await this.getWarehouse()
      await this.getPosition(this.data.item.warehouseId, this.data.item.positionId)
    },
    formSubmit: async function (e) {
        let data = e.detail.value,
            inStockData = wx.getStorageSync('inStockData') ? wx.getStorageSync('inStockData') : []
        if (data) {
            if(!data['warehouseId']){
              api.showToast('请选择仓库')
              return
            }
            if(!data['positionId']){
              api.showToast('请选择库位')
              return
            }
            data['money'] = (data['num']*data['price']).toFixed(app.globalData.setData.amount)
            // 初始化部分值
            if(this.data.key){
              inStockData[this.data.key] = data
            }else{
              inStockData.push(data)
            }
            api.showToast('选择成功')
            wx.setStorageSync('inStockData', inStockData)
            wx.navigateBack({
                delta: 1 //(this.data.from === 'add') ? 1 : 2
            })
        }
    },
    onStepperChange(obj) {
        this.setData({['item.num']: obj.detail})
    },
    inputValue(e) {  
      this.setData({  
        ['item.batch']: e.detail,
      });  
    },
    // 关闭
    onClose: function (e) {
      let obj = e.currentTarget.dataset
      if (obj) {
          this.setData({[obj.target]: false})
      }
    },
    funRouting(type, detail) {
      switch (type) {
          case 'orderDate':
              return api.formatDate(detail)
          case 'warehouseId':
              let warehouse = detail.value
              this.setData({
                  ['item.warehouse']: warehouse['text']
              })
              return warehouse['value']
          case 'positionId':
              let position = detail.value
              this.setData({
                  ['item.position']: position['text']
              })
              return position['value']
      }
    },
    // 确认选择
    onConfirm: async function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'item'
        let field = (prefix ? prefix + '.' : '') + obj.field
        let val = this.funRouting(obj.field, e.detail)
        this.setData({
            [obj.target]: false,
            [field]: val,
        })
        // 获取库位
        if(obj.field === 'warehouseId'){
          await this.getPosition(val, this.data.item.positionId)
        }
    },
    showPopup: function (obj) {
      let popup = obj.currentTarget.dataset.show
      if (popup) {
          this.setData({
              [popup]: true
          });
      }
    },
    onLoad: async function (obj) {
        const from = obj.from ? obj.from : '',
            string = obj.data ? obj.data : '',
            key = obj.key ? obj.key : ''
        let array = []
        try {
            array = api.jsonToArray(api.urlDecode(string))
            array['num'] = array['num'] == 0 ? 1 : array['num']
            this.setData({
                item: array,
                from: from,
                key: key
            })
        } catch (e) {
            wx.navigateBack({delta: 1})
            api.showToast('参数错误')
        }
        await this.main()
    }
});